package com.jms.product.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jms.common.exception.BizCodeEnum;
import com.jms.common.utils.HttpUtils;
import com.jms.common.utils.R;
import com.jms.product.constant.AuthServerConstant;
import com.jms.product.entity.UserBaseEntity;
import com.jms.product.feign.ThirdPartyFeignService;
import com.jms.product.service.AuthServerService;
import com.jms.product.service.UserBaseService;
import com.jms.product.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Jamison
 * @version 1.0
 * @date 2021/6/10 15:23
 */
@Slf4j
@RestController
public class AuthServerController {

    @Autowired
    private ThirdPartyFeignService thirdPartyFeignService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserBaseService userBaseService;

    @Autowired
    private AuthServerService authServerService;

    /**
     * 发送验证码
     * @param mobile
     * @return
     */
    @GetMapping("/sms/sendSms")
    public R sendSms(@RequestParam("mobile") String mobile) {
        String phonePre = AuthServerConstant.SMS_CODE_CACHE_PREFIX + mobile;
        String redisCode = redisTemplate.opsForValue().get(phonePre);
        // 接口防刷
        if (!StringUtils.isEmpty(redisCode)) {
            long l = Long.parseLong(redisCode.split("_")[1]);
            // 小于60秒就返回
            if ((System.currentTimeMillis() - l) < 60000) {
                return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(), BizCodeEnum.SMS_CODE_EXCEPTION.getMsg());
            }
        }
        // 生成一个4位数字的验证码
        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
        // 调用远程服务发送验证码
        try {
            thirdPartyFeignService.sendSms(mobile, code);
            redisTemplate.opsForValue().set(phonePre, code + "_" + System.currentTimeMillis(), 60, TimeUnit.DAYS);
        } catch (Exception e) {
            return R.error(BizCodeEnum.SMS_CODE_FAIL.getCode(), BizCodeEnum.SMS_CODE_FAIL.getMsg());
        }
        // 获取认证的类型
        UserBaseEntity one = userBaseService.getOne(new QueryWrapper<UserBaseEntity>().eq("user_name", mobile).or().eq("mobile", mobile));
        if (one != null){
            // 说明已经注册过了，就返回登录的提示
            return R.ok().setData(BizCodeEnum.AUTH_SERVER_TYPE_LOGIN.getMsg());
        }
        // 返回类型是注册
        return R.ok().setData(BizCodeEnum.AUTH_SERVER_TYPE_REGISTER.getMsg());
    }

    @PostMapping("/auth/getUser")
    public R getUser(HttpServletRequest request){
        UserBaseEntity userBaseEntity = (UserBaseEntity) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        return R.ok().setData(userBaseEntity);
    }






}
